package com.yjz.study.ui.biz

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.button.MaterialButton
import com.yjz.common.base.BaseActivity
import com.yjz.common.extend.back
import com.yjz.common.extend.getResStr
import com.yjz.common.extend.showToast
import com.yjz.common.restful.CallBack
import com.yjz.common.restful.HttpBaseResponse
import com.yjz.study.R
import com.yjz.study.arouter.ARouterLogic
import com.yjz.study.http.ApiFactory
import com.yjz.study.http.api.AccountApi
import com.yjz.ui.view.IconFontTextView
import com.yjz.ui.view.InputItemLayout

/**
 *作用 : 注册页面
 *作者 : yjz
 *时间 : 2025/2/14 14:37
 */
@Route(path = ARouterLogic.Path.REGISTER_PATH)
class RegisterActivity : BaseActivity() {

    override fun getLayoutId() = R.layout.activity_register

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        findViewById<IconFontTextView>(R.id.action_back).setOnClickListener { back() }
        findViewById<MaterialButton>(R.id.action_submit).setOnClickListener { submit() }
    }

    private fun submit() {
        // 3359
        val orderId = findViewById<InputItemLayout>(R.id.input_item_orderId).getTextContent()
        // 5733850
        val moocId = findViewById<InputItemLayout>(R.id.input_item_moocId).getTextContent()
        val username = findViewById<InputItemLayout>(R.id.input_item_username).getTextContent()
        val pwd = findViewById<InputItemLayout>(R.id.input_item_pwd).getTextContent()
        val pwdSec = findViewById<InputItemLayout>(R.id.input_item_pwd_check).getTextContent()

        if (TextUtils.isEmpty(orderId) || (TextUtils.isEmpty(moocId)) || (TextUtils.isEmpty(username))
            || TextUtils.isEmpty(pwd) || (!TextUtils.equals(pwd, pwdSec))
        ) {
            showToast(getResStr(R.string.input_empty_hint))
            return
        }

        ApiFactory.create(AccountApi::class.java).register(username, pwd, "5733850", "3359")
            .enqueue(object : CallBack<String> {
                override fun onSuccess(response: HttpBaseResponse<String>) {
                    showToast(response.msg)
                    if (response.successful()) {
                        //注册成功
                        val intent = Intent()
                        intent.putExtra("username", username)
                        setResult(Activity.RESULT_OK, intent)
                        finish()
                    } else {
                        showToast(response.msg)
                    }
                }

                override fun onFailed(throwable: Throwable) {
                    showToast(throwable.message)
                }
            })
    }
}